<?php
class indexMod extends commonMod
{
	public function __construct()
	{
		parent::__construct();
	}
	//后台首页
	public function index()
	{
		$pid=isset($_GET[0]) ? trim($_GET[0]) :'0';
		//$a=$_GET[0];
		//dump($a);
		//$this->assign('lang',LANG::getPack());
		$this->assign('pid',$pid);

		$condition['user_id']=$_SESSION['user_id'];
		$info=$this->model->table('users')->where($condition)->find();
		//echo $this->model->sql;
		$this->assign('user_name',$info['user_name']);
		$group=$this->model->table('group')->where("id='".$info['group_id']."'")->find();
		//echo $this->model->sql;
		//echo "<br>";
		$this->assign('group_name',$group['name']);

		$this->assign('nav_name',$this->model->field('name,url')->table('admin_nav')->where("id='".$pid."'")->find());

		//echo $this->model->sql;echo "<br>";
		$this->assign('admin_nav',$this->admin_nav('0'));
		//echo($this->admin_nav('0'));
		if($pid)
		{

			$this->assign('admin_two_nav',$this->admin_nav($pid));
			//dump($this->admin_nav($pid));
			$tz=$this->admin_nav($pid);
			//dump($tz[0]["url"]);
			//echo __APP__;
			$this->assign('iframe',__APP__.$tz[0]['url']);
		}
		else
		{
			$this->assign('iframe',__APP__."/index/right_main");
		}
		$this->display('index/index');
	}
	//内容部分
	public function right_main()
	{
		$ycms['upload_file']=ini_get('upload_max_filesize');
		$ycms['server_software']=$_SERVER['SERVER_SOFTWARE'];
		$ycms['catalog']=$_SERVER['DOCUMENT_ROOT'];
		$mysql=$this->model->query("SELECT VERSION()");
		$ycms['mysql']=$mysql['0']['VERSION()'];
		$this->assign('ycms',$ycms);
		$condition['user_id']=$_SESSION['user_id'];
		$info=$this->model->table('users')->where($condition)->find();
		$info['reg_time']=date('y-m-d h:i:s',$info['reg_time']);
		$this->assign('info',$info);
		$group=$this->model->table('group')->where("id='".$info['group_id']."'")->find();
		$this->assign('group_name',$group['name']);
		$this->assign('login_ip',$_SERVER["REMOTE_ADDR"]);
		$this->assign('lang',LANG::getPack('main'));
		$this->display('index/main');
	}
	//插件列表
	public function plugins()
	{
		$this->assign('lang',LANG::getPack('plugins'));
		$this->display('index/plugins');
		}
	//清除缓存
	public function clear_tpl()
	{

		 $dir = __ROOT__ . "/data/tpl_cache/"; //清除模板缓存
       		del_dir($dir);
		$this->success('模板缓存清除成功！',__URL__);

	}

	public function clearhtml()
    	{


        		$dir =__ROOT__ . "/data/html_cache/"; //清除html缓存
        		del_dir($dir);
       		$this->success('HTML缓存清除成功！',__URL__);
            }

    	public function cleardb()
    	{


        		$dir = __ROOT__ . "/data/db_cache/"; //清除数据库缓存
        		del_dir($dir);
       		$this->success('数据库缓存清除成功！',__URL__);
    	}
	//后台导航
	public function admin_nav($parent_id)
	{
		$condition['parent_id']=$parent_id;
		$list=$this->model->field('id,parent_id,name,url')->table('admin_nav')->where($condition)->order('show_order DESC')->select();
		return $list;
	}
	//后台登录
	public function login()
	{
		if(empty($_POST['login_yy']))
		{
			$this->display('index/login');
			return;
		}
		$username=in($_POST['user_name']);
		$password=in($_POST['user_password']);

		if(empty($username))
		{
			$this->error('请输入用户名！');
		}
		if(empty($password))
		{
			$this->error('请输入密码！');
		}
		if($_POST['checkcode']!=$_SESSION['verify'])
		{
			$this->error('验证码错误请重新输入！');
		}
		$condition['user_name']=$username;
		$info=$this->model->table('users')->where($condition)->find();

		if($info['user_password']==md5($password))
		{

			 //echo __APP__;
			 Auth::set($info['group_id']);
			 $this->model->table('users')->data("last_ip='".$_SERVER["REMOTE_ADDR"]."'")->where($condition)->update();
			 //echo $this->model->sql;
			 //echo "<br>";
			$_SESSION['user_id']=$info['user_id'];
			$_SESSION['user_name']=$info['user_name'];
			$this->redirect(__APP__);
			return  true;
		}else
		{
		    $this->error('密码输入错误！');
		}
	}
	//退出登录
	public function login_out()
	{
		Auth::clear();

		//$this->redirect(__APP__);
		//弹出等待退出框框
		$this->success("退出成功",'/login');


		//dump(__APP__);
		//echo "退出成功";
		//echo __APP__;
	}
	//验证码
	public function verify()
	{
		require_once(CP_PATH.'lib/Image.class.php');
		Image::buildImageVerify();
	}
}
?>